<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">

    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../plugins/healthmobile.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>权限管理
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>检查项管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建用户</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.gender == '1' ? '男' : '女'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="birthday" label="出生日期" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center"></el-table-column>
                <el-table-column prop="station" label="状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.station == '1' ? '已启用' : '已禁用'}}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="添加用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="addRules" label-position="right"
                                label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="电话号码" prop="telephone">
                                                <el-input v-model="formData.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.gender" placeholder="请选择">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <div class="block">
                                                    <el-date-picker
                                                            v-model="formData.birthday"
                                                            type="date"
                                                            value-format="yyyy-MM-dd"
                                                            format="yyyy-MM-dd"
                                                            placeholder="选择日期">
                                                    </el-date-picker>
                                                </div>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password" type="password">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>序号</th>
                                            <th>角色名</th>
                                            <th>角色关键字</th>
                                            <th>选择</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td><label :for="c.id">{{c.id}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="性别" prop="gender">
                                            <el-select v-model="formData.gender">
                                                <el-option label="男" value="1"></el-option>
                                                <el-option label="女" value="2"></el-option>
                                            </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="出生日期" prop="birthday">
                                            <div class="block">
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        type="date"
                                                        value-format="yyyy-MM-dd"
                                                        format="yyyy-MM-dd"
                                                        placeholder="选择日期">
                                                </el-date-picker>
                                            </div>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="12">
                                        <el-form-item label="电话号码" prop="telephone">
                                            <el-input v-model="formData.telephone"/>
                                        </el-form-item>
                                    </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="密码">
                                                <el-input v-model="formData.password" placeholder="重置密码" type="password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                    <el-col :span="12">
                                        <el-form-item label="状态">
                                            <template>
                                                <el-radio v-model="formData.station" label="1">启用</el-radio>
                                                <el-radio v-model="formData.station" label="2">禁用</el-radio>
                                            </template>
                                        </el-form-item>
                                    </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>序号</th>
                                            <th>角色名</th>
                                            <th>角色关键词</th>
                                            <th>选择</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>{{c.id}}</td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>


<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName:'first',
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//查询的表格数据
            tableData: [],
            roleIds: [],
            formData: {},//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//添加角色表单是否可见
            rules: {//校验规则
                username: [{required: true, message: '用户名为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '电话号码为必填项', trigger: 'blur'}]
            },
            addRules: {//新增校验规则
                username: [{required: true, message: '用户名为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '电话号码为必填项', trigger: 'blur'}],
                password:[{required: true, message: '密码不能为空', trigger: 'blur'}]
            }
        },
        //钩子函数,Vue对象初始化完成后自动执行
        created() {
            this._findPage();//VUE对象初始化完成后调用分页查询方法，完成分页查询
        },
        methods: {
            //重置添加用户表单的方法
            resetForm() {
                this.formData = {};
                this.roleIds = [];
            },
            //权限提示的方法
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error("无权限访问");
                    return;
                } else {
                    this.$message.error("未知错误");
                    return;
                }
            },
            //当页面切换的方法
            handleCurrentChange(currentPage) {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //bug修复,点击查询时先将当前页设置为第一页
            _findPage() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            findPage() {
                //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                axios.post("/user/findPage.do", param).then((res) => {
                    // var date = getSpecifiedDate(res.data.rows[0].birthday);
                    //查询成功,将数据赋值给前端页面
                    this.pagination.total = res.data.total;
                    this.dataList = res.data.rows;
                }).catch((r) => {
                    this.showMessage(r);
                });
            },
            //添加用户的方法
            handleAdd() {
                var telephone = this.formData.telephone;
                if (!checkTelephone(telephone)) {
                    //手机号输入不合法
                    this.$message.error("手机号不符合要求");
                    return false;
                }
                var password = this.formData.password;
                if (!checkPassword(password)) {
                    //密码不符合规则
                    this.$message.error("请将密码控制在4-16位之间");
                    return false;
                }
                this.$refs['dataAddForm'].validate((valid) => {
                    if (valid) {
                        //表单校验成功,发送ajax请求
                        // this.formData.birthday = getSpecifiedDate(this.formData.birthday);
                        axios.post("/user/addUser.do?roleIds="+this.roleIds, this.formData).then((res) => {
                            if (res.data.flag) {
                                //添加成功,关闭新增页面
                                this.dialogFormVisible = false;
                                //获得成功响应,重新调用查询页面的方法
                                this._findPage();
                                this.$message({
                                    message: res.data.message,
                                    type: 'success'
                                });
                            } else {
                                //添加失败
                                this.$message.error(res.data.message);
                            }
                        }).catch((r) => {
                            this.showMessage(r);
                        });
                    }
                });
            },
            //弹出编辑用户窗口
            handleUpdate(row) {
                this.activeName="first";
                //重置数据
                this.resetForm();
                if (this.$refs['dataEditForm'] != undefined) {
                    this.$refs['dataEditForm'].clearValidate();
                }
                //发送请求,查询用户数据,用于页面回显
                axios.post("/user/findById.do?id="+row.id).then((res)=>{
                    if(res.data.flag){
                        //查询到数据
                        this.formData = res.data.data;
                    }else{
                        //查询数据失败
                        this.$message.error(res.data.message);
                    }
                });
                //发送ajax请求，查询所有的角色数据，用于展示角色列表
                axios.get("/role/findAll.do").then((res) => {
                    if(res.data.flag){
                        //查询角色数据成功
                        this.tableData = res.data.data;
                        //发送ajax请求，根据用户ID查询当前用户包含的角色ID，用于页面复选框回显
                        axios.get("/role/findRoleByUserId.do?id=" + row.id).then((res) => {
                                if(res.data.flag){
                                this.roleIds = res.data.data;
                            }else{
                                this.$message.error(res.data.message);
                            }
                        });
                    }else{
                        //查询检查项数据失败
                        this.$message.error(res.data.message);
                    }
                });
                //弹出窗口
                this.dialogFormVisible4Edit = true;
            },
            //删除用户的方法
            handleDelete(row) {
                this.$confirm("您确定要删除此用户吗", "提示", {
                    type: "warning"
                }).then(() => {
                    //确认删除,发送ajax请求
                    axios.post("/user/deleteById.do?id=" + row.id).then((res) => {
                        if (res.data.flag) {
                            //删除成功
                            this.$message({
                                message: res.data.message,
                                type: "success"
                            });
                            //删除成功分页查询
                            this._findPage();
                        } else {
                            //删除失败
                            this.$message.error(res.data.message);
                        }
                    }).catch((r) => {
                        this.showMessage(r);
                    })
                }).catch(() => {
                    //取消删除
                    this.$message({
                        message: "已取消",
                        type: "info"
                    })
                });
            },
            //弹出添加用户窗口
            handleCreate() {
                this.activeName = "first";
                //重置数据
                this.resetForm();
                if (this.$refs['dataAddForm'] != undefined) {
                    this.$refs['dataAddForm'].clearValidate();
                }
                //发送请求查询角色信息,用于回显页面
                axios.post("/role/findAll.do?").then((res)=>{
                   if (res.data.flag) {
                       this.tableData = res.data.data;
                   }
                });
                this.dialogFormVisible = true;
            },
            //提交修改
            handleEdit(){
                //进行表单校验
                this.$refs['dataEditForm'].validate((valid) => {
                    if (valid){
                        axios.post("/user/edit.do?roleIds=" + this.roleIds,this.formData).then((res) => {
                            //关闭编辑窗口
                            this.dialogFormVisible4Edit = false;
                            if(res.data.flag){
                                //操作成功
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                            }else{
                                //操作失败
                                this.$message.error(res.data.message);
                            }
                        }).finally(() =>{
                            this.findPage();
                        });
                    }else{
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                })
            }
        }
    })
</script>
</html>